package redis.inaction.xx.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.inaction.xx.cache.SessionRedisCache;

import java.util.List;

@Configuration
@AutoConfigureAfter(BizRedisConfiguration.class)
public class SessionRedisConfiguration {

    @Value("${spring.redis.session.max-redirects}")
    private int maxRedirects = 3;
    @Value("${spring.redis.session.max-active}")
    private int maxTotal = 8;
    @Value("${spring.redis.session.max-idle}")
    private int maxIdle = 8;
    @Value("${spring.redis.session.min-idle}")
    private int minIdle = 0;

    @Bean(name = "sessionRedisTemplate")
    public RedisTemplate<String, byte[]> sessionRedisTemplate(
            @Qualifier("sessionJedisConnectionFactory")
                    RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(RedisSerializer.byteArray());
        return template;
    }

    @Bean
    public RedisNodeLoader redisNodeLoader(){
        return new RedisNodeLoader();
    }

    @Bean("sessionJedisPoolConfig")
    @ConfigurationProperties(prefix = "spring.redis.session")
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMinIdle(minIdle);
        return poolConfig;
    }

    @Bean("sessionJedisConnectionFactory")
    public JedisConnectionFactory jedisConnectionFactory(){
        List<String> nodes = redisNodeLoader().loadNodes();
        return createJedisConnectionFactory(nodes, jedisPoolConfig());
    }

    @Bean
    public SessionRedisCache sessionRedisCache(){
        return new SessionRedisCache();
    }

   private JedisConnectionFactory createJedisConnectionFactory(List<String> redisClusterNodes, JedisPoolConfig poolConfig) {
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(redisClusterNodes);
        clusterConfiguration.setMaxRedirects(maxRedirects);
        return new JedisConnectionFactory(clusterConfiguration, poolConfig);
    }
}
